package com.example.demo.controller;

import com.example.demo.jobs.DemoJob;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import static org.quartz.CronScheduleBuilder.cronSchedule;

/**
 * 类注释
 *
 * @Author: dyf
 * @Date:
 */
@Controller
public class Test {

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    @GetMapping("/")
    public  String scheduleJobs() throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();

        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("jobArg", "world");
        JobDetail jobDetail = JobBuilder.newJob(DemoJob.class)
                .setJobData(jobDataMap)
                .withDescription("demo")
                .withIdentity("demo-job", "demo-group")
                .build();

        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(jobDetail)
                .withSchedule(cronSchedule("0/5 * * * * ?"))
                .build();
        if(!scheduler.checkExists(JobKey.jobKey("demo-job","demo-group"))){
            scheduler.scheduleJob(jobDetail,trigger);
        }
        scheduler.start();

        return "aa";
    }

}
